package com.gopay.checkorder.service.task;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.manager.TpasOrderManager;
import com.gopay.checkorder.service.task.thread.CheckWachgeOrderTaskThread;
import com.gopay.common.domain.bank.tpas.TpasOrder;
import com.gopay.common.util.ExecutorUtil;

public class CheckWachgeOrderTaskImpl implements Task {
	private final static Logger logger = LoggerFactory.getLogger("CHECK_ORDER");

	private final static int MAX_SIZE = 1000;

    @Resource(name = "tpasOrderManager")
    private TpasOrderManager tpasOrderManager;
    
	@Resource(name = "checkWachgeOrderTaskThread")
	private CheckWachgeOrderTaskThread checkWachgeOrderTaskThread;
	
	@Override
	public void doTask() {
	    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10, 3, TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(100), new ThreadPoolExecutor.CallerRunsPolicy());

        try{
            int pageSize = 100;
            int pageIndex = 0;
            int count = 0;
    
            int resCount = getCheckWachgeOrderCount();
            logger.error("定时任务tpas checkorder running!!! 对单数量为: "+resCount);
            List<TpasOrder> list = null;
            while (count < resCount) {
                list = getCheckWachgeOrder(pageIndex, pageSize);

                if (null != list && list.size() > 0) {
                    logger.error("checkorder list size: " + list.size());
                    /*注释掉测试日志输出代码 zhoutong 20171010
                    // 增加测试日志输出
                    for (int i = 0; i < list.size(); i++) {
                        TpasOrder tpasOrder = (TpasOrder) list.get(i);
                        logger.error("checkorder test orderno: " + tpasOrder.getOrderId() + " orderstatus: " + tpasOrder.getOrderStatus()
                                + " totalchecktimes: " + tpasOrder.getTotalCheckTimes());
                    }
                    */
                } else {
                    logger.error("no tpasorder data.");
                }

                LinkedBlockingQueue<TpasOrder> queue = new LinkedBlockingQueue<TpasOrder>();
                queue.addAll(list);
    
                int size = queue.size();
                CountDownLatch latch = new CountDownLatch(size);
    
                checkWachgeOrderTaskThread.setLatch(latch);
                checkWachgeOrderTaskThread.setQueue(queue);
    
                for (int i = 0; i < list.size(); i++) {
                    TpasOrder tpasOrder = (TpasOrder)list.get(i);
                    if(tpasOrder !=null){
                        BankCountDownLatch.totalIncrease(tpasOrder.getIntrfaceChannelCode());              
                    }               
                    threadPool.execute(checkWachgeOrderTaskThread);
                }
    
                count += pageSize;
                pageIndex++;
    
                try {
                    latch.await(5, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    logger.error(getClass().getName() + " doTask fail.", e);
                }
            }
        }catch (Throwable t) {
            logger.error("执行对单Task时出现异常" ,t);
        }finally{
            ExecutorUtil.gracefulShutdown(threadPool, 1000 * 60 * 3);
        }        
	}

    private List<TpasOrder> getCheckWachgeOrder(int pageIndex, int pageSize) {
        List<TpasOrder> list = tpasOrderManager.getCheckWachgeOrder(pageIndex, pageSize);
        return list;
    }

    private int getCheckWachgeOrderCount() {
        int resCount = tpasOrderManager.getCheckWachgeOrderCount();
        if (resCount > MAX_SIZE) {
            resCount = MAX_SIZE;
        }
        return resCount;
    }
}
